package com.project.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.project.bean.UserBean;
import com.project.service.UserService;
import com.project.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FindAllServlet extends HttpServlet {
    UserService service = new  UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

//        调用业务层方法
        List<UserBean> list = service.findAll();
//        产生jackson对象
        ObjectMapper om = new ObjectMapper();
//        注册日期格式化模块
        om.registerModule(new JavaTimeModule());
//        将对象转化为json字符串
        String str = om.writeValueAsString(list);
        resp.getWriter().print(str);
    }
}
